package com.example.user.viewmodel;

import android.os.Bundle;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.common.component.LoginCtrlCom;
import com.example.common.component.UserCtrlCom;
import com.example.common.control.ICallback;


import com.example.common.model.Description;
import com.example.common.model.SessionApiLoginResp;
import com.example.common.user.GlobalUser;
import com.example.user.model.UserDetail;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


public class UserMyProfileDetailViewModel extends ViewModel {

    private Description description = new Description();//接受值
    private UserCtrlCom userCtrlCom = new UserCtrlCom();
    private MutableLiveData<Boolean> isLoad = new MutableLiveData<>();
    private SimpleDateFormat simpleDateFormat1;//日期格式化
    private SimpleDateFormat simpleDateFormat2;//日期格式化
    private MutableLiveData<Boolean> isNeedToLogin=new MutableLiveData<>();//按钮状态


    private UserDetail userDetail = new UserDetail();
    public static String TOKEN = "";


    public UserMyProfileDetailViewModel() {
        initData();
    }

    /**
     * 数据初始化
     */
    private void initData() {
        //数据初始化
        userDetail.getIntroduction().setValue("暂时没有介绍哦～");
        userDetail.getBirthday().setValue("2021 - 1 - 1");
        userDetail.getGender().setValue(0);
        simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        //在线获取用户资料
        getUserDetailOnline();
        isNeedToLogin.setValue(true);
    }

    /**
     * 在线获取用户详细信息
     */
    public void getUserDetailOnline() {
        isLoad.setValue(false);
        userCtrlCom.getUser(GlobalUser.TOKEN, new ICallback() {
            @Override
            public void onSuccess(Object response) {
                description = (Description) response;
                //用户信息获取保存
                userDetail.getAvatarUrl().setValue(description.getAvatar());//头像图片url
                userDetail.getUserName().setValue(description.getUsername());//用户名
                userDetail.getIntroduction().setValue(description.getIntroduction());//个人简介
                if (description.getBirthday() != null) {
                    try {
                        Date date = simpleDateFormat1.parse(description.getBirthday());
                        userDetail.getBirthday().setValue(simpleDateFormat2.format(date));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                } else {
                    userDetail.getBirthday().setValue(description.getBirthday());//生日
                }
                userDetail.getGender().setValue(description.getGender());
                //设置ID
                GlobalUser.ID=description.getId();
                //设置按钮状态
                isNeedToLogin.setValue(false);
                //加载是否完成
                isLoad.setValue(true);
            }

            @Override
            public void onFailed(String mes) {
                Log.d("TAG", "onFailed: 未登录");
            }
        });
    }


    public UserDetail getUserDetail() {
        return userDetail;
    }

    /**
     * 传递给下一个页面的数据
     *
     * @return
     */
    public Bundle translateBundle() {
        Bundle bundle = new Bundle();
        bundle.putString("USERNAME", userDetail.getUserName().getValue());   //用户名
        bundle.putString("INTRODUCTION", userDetail.getIntroduction().getValue());   //简介
        bundle.putInt("GENDER", userDetail.getGender().getValue());  //性别
        bundle.putString("BIRTHDAY", userDetail.getBirthday().getValue());   //生日
        bundle.putString("AVATAR", userDetail.getAvatarUrl().getValue()); //头像地址
        return bundle;
    }

    public MutableLiveData<Boolean> getIsNeedToLogin() {
        return isNeedToLogin;
    }

    public MutableLiveData<Boolean> getIsLoad() {
        return isLoad;
    }
}
